{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84b3309f",
   "metadata": {},
   "source": [
    "# 爬中文文献"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60ee7dd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.common.exceptions import NoSuchElementException\n",
    "import pandas as pd\n",
    "import csv\n",
    "import os\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫\\\\文献爬虫数据') # 设置当前工作目录为你要保存文件的目录\n",
    "keyword='esg' # 输入检索词\n",
    "# 初始化WebDriver\n",
    "driver = webdriver.Edge()\n",
    "url = 'https://kns.cnki.net/kns8s/AdvSearch?crossids=YSTT4HG0%2CLSTPFY1C%2CJUP3MUPD%2CMPMFIG1A%2CWQ0UVIAA%2CBLZOG7CK%2CPWFIRAGL%2CEMRPGLPA%2CNLBO1Z6R%2CNN3FJMUV'\n",
    "driver.get(url)\n",
    "time.sleep(2)\n",
    "\n",
    "\n",
    "# 点击知网检索输入栏，输入你的检索词\n",
    "driver.find_element(By.XPATH, '//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').send_keys(keyword) #检索词\n",
    "time.sleep(2)\n",
    "\n",
    "# 进行检索//*                     \n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[1]/div/div[2]/div/div[1]/div/div[1]/div[2]/div[3]/input').click()\n",
    "time.sleep(4)\n",
    "# 显示详细信息，为了爬取摘要、关键词\n",
    "#driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div/div[1]/div/div[2]/ul[2]/li[1]/i').click()\n",
    "#time.sleep(2)\n",
    "\n",
    "\n",
    "# 每页显示条数改成50\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/div/i').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/ul/li[3]/a').click()\n",
    "time.sleep(3)\n",
    "\n",
    "\n",
    "#只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click()\n",
    "time.sleep(2)\n",
    "# 点击“学术论文”\n",
    "\n",
    "# 创建空列表以存储所有元素的文本内容\n",
    "column1_texts = []\n",
    "column2_texts = []\n",
    "column3_texts = []\n",
    "column4_texts = []\n",
    "column5_texts = []\n",
    "column6_texts = []\n",
    "column7_texts = []\n",
    "a=1\n",
    "# 循环遍历每一页\n",
    "while True:\n",
    "    # 获取当前页的元素数量  //*[@id=\"gridTable\"]/div/div/div/table/tbody\n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr'))\n",
    "    if elements_count != 50:\n",
    "        print(f\"当前页{a}元素数量: {elements_count}，不是50，可能是最后一页\")\n",
    "    # 遍历当前页的元素，获取文本内容并存储到列表中\n",
    "    for i in range(1, elements_count + 1):\n",
    "        # 第一列 文章题目      //*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[1]/td[2]/a\n",
    "        try:\n",
    "            element_col1 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[2]/a')\n",
    "            column1_texts.append(element_col1.text)\n",
    "            column7_texts.append(element_col1.get_attribute('href')) # 获取链接\n",
    "        except NoSuchElementException:\n",
    "            column1_texts.append(\"NA\")\n",
    "\n",
    "        # 第二列 作者      \n",
    "        try:\n",
    "            element_col2 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[3]')\n",
    "            column2_texts.append(element_col2.text)\n",
    "        except NoSuchElementException:\n",
    "            column2_texts.append(\"NA\")\n",
    "\n",
    "        # 第三列 期刊        \n",
    "        try:\n",
    "            element_col3 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[4]/span/a')\n",
    "            column3_texts.append(element_col3.text)\n",
    "        except NoSuchElementException:\n",
    "            column3_texts.append(\"NA\")\n",
    "\n",
    "        # 第四列 时间        \n",
    "        try:\n",
    "            element_col4 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[5]')\n",
    "            column4_texts.append(element_col4.text)\n",
    "        except NoSuchElementException:\n",
    "            column4_texts.append(\"NA\")\n",
    "\n",
    "        # 第五列 被引      \n",
    "        try:\n",
    "            element_col5 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[6]')\n",
    "            column5_texts.append(element_col5.text)\n",
    "        except NoSuchElementException:\n",
    "            column5_texts.append(\"NA\")\n",
    "\n",
    "        # 第六列 下载       \n",
    "        try:\n",
    "            element_col6 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[7]')\n",
    "            column6_texts.append(element_col6.text)\n",
    "        except NoSuchElementException:\n",
    "            column6_texts.append(\"NA\")\n",
    "\n",
    "\n",
    "\n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        print(f'正在爬一轮第{a}页')\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        break  # 如果找不到下一页按钮，跳出循环 \n",
    "    \n",
    "df1=pd.DataFrame({\n",
    "    '题名': column1_texts,\n",
    "    '链接': column7_texts,\n",
    "    '作者': column2_texts,\n",
    "    '期刊名称': column3_texts,\n",
    "    '时间': column4_texts,\n",
    "    '被引': column5_texts,\n",
    "    '下载': column6_texts\n",
    "    })\n",
    "\n",
    "\n",
    "driver.find_element(By.ID, 'total').click() #点击首页\n",
    "time.sleep(2)  # 等待页面加载\n",
    "\n",
    "# 显示详细信息，为了爬取摘要、关键词\n",
    "driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div/div[1]/div/div[2]/ul[2]/li[1]/i').click()\n",
    "time.sleep(2)\n",
    "\n",
    "\n",
    "\n",
    "#爬摘要和关键词\n",
    "title_texts = []\n",
    "abstract_texts = []\n",
    "keywords_texts = []\n",
    "a = 1 # 页码计数器\n",
    "\n",
    "while True:\n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/dd'))\n",
    "    if elements_count !=50:\n",
    "        try:\n",
    "            elements_count_contain = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/font/dd'))\n",
    "            elements_count = elements_count_contain + elements_count\n",
    "            print(f\"当前页{a}的1叠元素数量: {elements_count}\")\n",
    "            if elements_count != 50:\n",
    "                elements_count_contain2 = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd'))\n",
    "                elements_count = elements_count_contain2 + elements_count\n",
    "                print(f\"当前页{a}的2叠元素数量: {elements_count}\")\n",
    "        except NoSuchElementException:\n",
    "            elements_count = elements_count\n",
    "            print(f\"当前页{a}的元素数量: {elements_count}，不是50个，可能是最后一页\")\n",
    "\n",
    "    b=c=1\n",
    "    for i in range(1, elements_count + 1):\n",
    "        #题目路径\n",
    "        xpath1=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/h6/a'\n",
    "        xpath2=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/h6/a'\n",
    "        xpath3=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/h6/a'\n",
    "        #摘要路径 \n",
    "        xpath4=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/p[2]/span[2]'\n",
    "        xpath5=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/p[2]/span[2]'\n",
    "        xpath6=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/p[2]/span[2]'\n",
    "        #关键词路径 \n",
    "        xpath7=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/p[3]'\n",
    "        xpath8=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/p[3]'\n",
    "        xpath9=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/p[3]'\n",
    "        #获取题目\n",
    "        try:\n",
    "            title_text = driver.find_element(By.XPATH, xpath1) \n",
    "            title_texts.append(title_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                title_text = driver.find_element(By.XPATH, xpath2) \n",
    "                title_texts.append(title_text.text)\n",
    "                b+=1 \n",
    "            except:\n",
    "                try:\n",
    "                    title_text = driver.find_element(By.XPATH,xpath3)\n",
    "                    title_texts.append(title_text.text)\n",
    "                    c+=1\n",
    "                except:\n",
    "                    title_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到标题\")\n",
    "            \n",
    "        #获取摘要\n",
    "        try:\n",
    "            abstract_text = driver.find_element(By.XPATH, xpath4) \n",
    "            abstract_texts.append(abstract_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                abstract_text = driver.find_element(By.XPATH, xpath5) \n",
    "                abstract_texts.append(abstract_text.text)\n",
    "            except:\n",
    "                try:\n",
    "                    abstract_text = driver.find_element(By.XPATH,xpath6)\n",
    "                    abstract_texts.append(abstract_text.text)\n",
    "                except:\n",
    "                    abstract_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到摘要\")\n",
    "        \n",
    "        #获取关键词\n",
    "        try:\n",
    "            keywords_text = driver.find_element(By.XPATH, xpath7) \n",
    "            keywords_texts.append(keywords_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                keywords_text = driver.find_element(By.XPATH, xpath8) \n",
    "                keywords_texts.append(keywords_text.text)\n",
    "            except:\n",
    "                try:\n",
    "                    keywords_text = driver.find_element(By.XPATH,xpath9)\n",
    "                    keywords_texts.append(keywords_text.text)\n",
    "                except:\n",
    "                    keywords_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到关键词\")\n",
    "    print(f'正在爬第{a}页')\n",
    "    \n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        break  # 如果找不到下一页按钮，跳出循环\n",
    "keywords_texts = [text.replace(\"关键词：\", \"\") for text in keywords_texts] # 去掉关键词前的“关键词：”\n",
    "\n",
    "df2=pd.DataFrame({\n",
    "    '题名': title_texts,\n",
    "    '摘要': abstract_texts,\n",
    "    '关键词': keywords_texts\n",
    "})\n",
    "driver.quit()# 关闭浏览器\n",
    "\n",
    "#合并数据\n",
    "df3 = pd.merge(df1, df2, on='题名', how='left')\n",
    "\n",
    "\n",
    "#读期刊目录\n",
    "excel_df = pd.read_excel(r'D:\\东财中文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df3, excel_df, on='期刊名称', how='left')\n",
    "from datetime import datetime\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间', '被引', '下载', '摘要', '关键词']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_中文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e63f8c72",
   "metadata": {},
   "source": [
    "# 爬外文文献"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d19add8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.common.exceptions import NoSuchElementException\n",
    "import pandas as pd\n",
    "import csv\n",
    "import os\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫\\\\文献爬虫数据') # 设置当前工作目录为你要保存文件的目录\n",
    "\n",
    "# 初始化WebDriver\n",
    "driver = webdriver.Edge()\n",
    "url = 'https://kns.cnki.net/kns8s/AdvSearch?crossids=YSTT4HG0%2CLSTPFY1C%2CJUP3MUPD%2CMPMFIG1A%2CWQ0UVIAA%2CBLZOG7CK%2CPWFIRAGL%2CEMRPGLPA%2CNLBO1Z6R%2CNN3FJMUV'\n",
    "driver.get(url)\n",
    "time.sleep(2)\n",
    "\n",
    "keyword='漂绿' \n",
    "# 点击知网检索输入栏，输入你的检索词\n",
    "driver.find_element(By.XPATH, '//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').send_keys(keyword) #检索词\n",
    "time.sleep(3)\n",
    "\n",
    "# 进行检索\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[1]/div/div[2]/div/div[1]/div/div[1]/div[2]/div[3]/input').click()\n",
    "time.sleep(4)\n",
    "# 每页显示条数改成50\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/div/i').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/ul/li[3]/a').click()\n",
    "time.sleep(3)\n",
    "\n",
    "\n",
    "#只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 点外文\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/div/div/a[2]').click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 创建空列表以存储所有元素的文本内容\n",
    "column1_texts = []\n",
    "column2_texts = []\n",
    "column3_texts = []\n",
    "column4_texts = []\n",
    "column5_texts = []\n",
    "\n",
    "a=1\n",
    "# 循环遍历每一页\n",
    "while True:\n",
    "    # 获取当前页的元素数量  //*[@id=\"gridTable\"]/div/div/div/table/tbody\n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr'))\n",
    "    if elements_count != 50:\n",
    "        print(f\"当前页{a}元素数量: {elements_count}，不是50，可能是最后一页\")\n",
    "    # 遍历当前页的元素，获取文本内容并存储到列表中\n",
    "    for i in range(1, elements_count + 1):\n",
    "        # 第一列 文章题目    \n",
    "        try:\n",
    "            element_col1 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[2]/a')\n",
    "            column1_texts.append(element_col1.text)\n",
    "            column5_texts.append(element_col1.get_attribute('href')) # 获取链接\n",
    "        except NoSuchElementException:\n",
    "            column1_texts.append(\"NA\")\n",
    "\n",
    "        # 第二列 作者      \n",
    "        try:\n",
    "            element_col2 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[3]')\n",
    "            column2_texts.append(element_col2.text)\n",
    "        except NoSuchElementException:\n",
    "            column2_texts.append(\"NA\")\n",
    "\n",
    "        # 第三列 期刊        \n",
    "        try:\n",
    "            element_col3 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[4]')\n",
    "            column3_texts.append(element_col3.text)\n",
    "        except NoSuchElementException:\n",
    "            column3_texts.append(\"NA\")\n",
    "\n",
    "        # 第四列 时间        \n",
    "        try:\n",
    "            element_col4 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[5]')\n",
    "            column4_texts.append(element_col4.text)\n",
    "        except NoSuchElementException:\n",
    "            column4_texts.append(\"NA\")\n",
    "\n",
    "\n",
    "\n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        print(f'正在爬起第{a}页')\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        break  # 如果找不到下一页按钮，跳出循环 \n",
    "    \n",
    "df1=pd.DataFrame({\n",
    "    '题名': column1_texts,\n",
    "    '链接': column5_texts,\n",
    "    '作者': column2_texts,\n",
    "    '期刊名称': column3_texts,\n",
    "    '时间': column4_texts,\n",
    "    })\n",
    "\n",
    "\n",
    "driver.find_element(By.ID, 'total').click() #点击首页\n",
    "time.sleep(2)  # 等待页面加载\n",
    "\n",
    "# 显示详细信息，为了爬取摘要、关键词\n",
    "driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div/div[1]/div/div[2]/ul[2]/li[1]/i').click()\n",
    "time.sleep(2)\n",
    "\n",
    "\n",
    "\n",
    "#爬摘要和关键词\n",
    "title_texts = []\n",
    "abstract_texts = []\n",
    "keywords_texts = []\n",
    "a = 1 # 页码计数器\n",
    "\n",
    "while True:\n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/dd'))\n",
    "    if elements_count !=50:\n",
    "        try:\n",
    "            elements_count_contain = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/font/dd'))\n",
    "            elements_count = elements_count_contain + elements_count\n",
    "            print(f\"当前页{a}的1叠元素数量: {elements_count}\")\n",
    "            if elements_count != 50:\n",
    "                elements_count_contain2 = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd'))\n",
    "                elements_count = elements_count_contain2 + elements_count\n",
    "                print(f\"当前页{a}的2叠元素数量: {elements_count}\")\n",
    "        except NoSuchElementException:\n",
    "            elements_count = elements_count\n",
    "            print(f\"当前页{a}的元素数量: {elements_count}，不是50个，可能是最后一页\")\n",
    "\n",
    "    b=c=1\n",
    "    for i in range(1, elements_count + 1):\n",
    "        #题目路径\n",
    "        xpath1=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/h6/a'\n",
    "        xpath2=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/h6/a'\n",
    "        xpath3=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/h6/a'\n",
    "        #摘要路径 \n",
    "        xpath4=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/p[2]/span[2]'\n",
    "        xpath5=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/p[2]/span[2]'\n",
    "        xpath6=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/p[2]/span[2]'\n",
    "        #关键词路径 \n",
    "        xpath7=f'//*[@id=\"gridTable\"]/div/div/div/dl/dd[{i}]/div[2]/p[3]'\n",
    "        xpath8=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/dd[{b}]/div[2]/p[3]'\n",
    "        xpath9=f'//*[@id=\"gridTable\"]/div/div/div/dl/font/font/dd[{c}]/div[2]/p[3]'\n",
    "        #获取题目\n",
    "        try:\n",
    "            title_text = driver.find_element(By.XPATH, xpath1) \n",
    "            title_texts.append(title_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                title_text = driver.find_element(By.XPATH, xpath2) \n",
    "                title_texts.append(title_text.text)\n",
    "                b+=1 \n",
    "            except:\n",
    "                try:\n",
    "                    title_text = driver.find_element(By.XPATH,xpath3)\n",
    "                    title_texts.append(title_text.text)\n",
    "                    c+=1\n",
    "                except:\n",
    "                    title_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到标题\")\n",
    "            \n",
    "        #获取摘要\n",
    "        try:\n",
    "            abstract_text = driver.find_element(By.XPATH, xpath4) \n",
    "            abstract_texts.append(abstract_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                abstract_text = driver.find_element(By.XPATH, xpath5) \n",
    "                abstract_texts.append(abstract_text.text)\n",
    "            except:\n",
    "                try:\n",
    "                    abstract_text = driver.find_element(By.XPATH,xpath6)\n",
    "                    abstract_texts.append(abstract_text.text)\n",
    "                except:\n",
    "                    abstract_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到摘要\")\n",
    "        \n",
    "        #获取关键词\n",
    "        try:\n",
    "            keywords_text = driver.find_element(By.XPATH, xpath7) \n",
    "            keywords_texts.append(keywords_text.text)\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                keywords_text = driver.find_element(By.XPATH, xpath8) \n",
    "                keywords_texts.append(keywords_text.text)\n",
    "            except:\n",
    "                try:\n",
    "                    keywords_text = driver.find_element(By.XPATH,xpath9)\n",
    "                    keywords_texts.append(keywords_text.text)\n",
    "                except:\n",
    "                    keywords_texts.append(\"NA\")\n",
    "                    print(f\"当前页{a}的元素数量: {elements_count}，第{i}个元素没有找到关键词\")\n",
    "    print(f'正在爬第{a}页')\n",
    "    \n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        break  # 如果找不到下一页按钮，跳出循环\n",
    "keywords_texts = [text.replace(\"关键词：\", \"\") for text in keywords_texts] # 去掉关键词前的“关键词：”\n",
    "\n",
    "df2=pd.DataFrame({\n",
    "    '题名': title_texts,\n",
    "    '摘要': abstract_texts,\n",
    "    '关键词': keywords_texts\n",
    "})\n",
    "driver.quit()# 关闭浏览器\n",
    "\n",
    "#合并数据\n",
    "df3 = pd.merge(df1, df2, on='题名', how='left')\n",
    "\n",
    "#读期刊目录\n",
    "excel_df = pd.read_excel(r'D:\\东财外文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df3, excel_df, on='期刊名称', how='left')\n",
    "\n",
    "\n",
    "\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间', '摘要', '关键词']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "from datetime import datetime\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_外文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "956c03bc",
   "metadata": {},
   "source": [
    "# 爬中文文献（不包含摘要关键词）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2e706998",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前页1元素数量: 20，不是50，可能是最后一页\n"
     ]
    },
    {
     "ename": "StaleElementReferenceException",
     "evalue": "Message: stale element reference: stale element not found in the current frame\n  (Session info: MicrosoftEdge=135.0.3179.73); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#stale-element-reference-exception\nStacktrace:\n\tGetHandleVerifier [0x00007FF7465D7A55+24981]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74652AC60+444240]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF74685E0EA+2011274]\n\t(No symbol) [0x00007FF746302430]\n\t(No symbol) [0x00007FF746305046]\n\t(No symbol) [0x00007FF746387343]\n\t(No symbol) [0x00007FF74636909A]\n\t(No symbol) [0x00007FF74633E13D]\n\t(No symbol) [0x00007FF7463867E8]\n\t(No symbol) [0x00007FF746368E03]\n\t(No symbol) [0x00007FF74633D686]\n\t(No symbol) [0x00007FF74633CB92]\n\t(No symbol) [0x00007FF74633D4B3]\n\t(No symbol) [0x00007FF74643900D]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF746446943+41539]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74643FC79+13689]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF7466BA04A+290794]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746537D31+497697]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530BB4+468644]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530D03+468979]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746522E16+411910]\n\tBaseThreadInitThunk [0x00007FFA2EDB53E0+16]\n\tRtlUserThreadStart [0x00007FFA3014485B+43]\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mStaleElementReferenceException\u001b[0m            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[14], line 79\u001b[0m\n\u001b[0;32m     76\u001b[0m     column1_texts\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNA\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     78\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 79\u001b[0m     column7_texts\u001b[38;5;241m.\u001b[39mappend(\u001b[43mdriver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfind_element\u001b[49m\u001b[43m(\u001b[49m\u001b[43mBy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mXPATH\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m//*[@id=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgridTable\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m]/div/div/div/table/tbody/tr[\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mi\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m]/td[2]/a\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_attribute\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhref\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m)\n\u001b[0;32m     80\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m NoSuchElementException:\n\u001b[0;32m     81\u001b[0m     column7_texts\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNA\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\webelement.py:178\u001b[0m, in \u001b[0;36mWebElement.get_attribute\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m    176\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m getAttribute_js \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    177\u001b[0m     _load_js()\n\u001b[1;32m--> 178\u001b[0m attribute_value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_script\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    179\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/* getAttribute */return (\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mgetAttribute_js\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m).apply(null, arguments);\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\n\u001b[0;32m    180\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    181\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m attribute_value\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:444\u001b[0m, in \u001b[0;36mWebDriver.execute_script\u001b[1;34m(self, script, *args)\u001b[0m\n\u001b[0;32m    441\u001b[0m converted_args \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(args)\n\u001b[0;32m    442\u001b[0m command \u001b[38;5;241m=\u001b[39m Command\u001b[38;5;241m.\u001b[39mW3C_EXECUTE_SCRIPT\n\u001b[1;32m--> 444\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mscript\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mscript\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43margs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mconverted_args\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:384\u001b[0m, in \u001b[0;36mWebDriver.execute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    382\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcommand_executor\u001b[38;5;241m.\u001b[39mexecute(driver_command, params)\n\u001b[0;32m    383\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m response:\n\u001b[1;32m--> 384\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43merror_handler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    385\u001b[0m     response[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_unwrap_value(response\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[0;32m    386\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py:232\u001b[0m, in \u001b[0;36mErrorHandler.check_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    230\u001b[0m         alert_text \u001b[38;5;241m=\u001b[39m value[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124malert\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    231\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace, alert_text)  \u001b[38;5;66;03m# type: ignore[call-arg]  # mypy is not smart enough here\u001b[39;00m\n\u001b[1;32m--> 232\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace)\n",
      "\u001b[1;31mStaleElementReferenceException\u001b[0m: Message: stale element reference: stale element not found in the current frame\n  (Session info: MicrosoftEdge=135.0.3179.73); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#stale-element-reference-exception\nStacktrace:\n\tGetHandleVerifier [0x00007FF7465D7A55+24981]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74652AC60+444240]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF74685E0EA+2011274]\n\t(No symbol) [0x00007FF746302430]\n\t(No symbol) [0x00007FF746305046]\n\t(No symbol) [0x00007FF746387343]\n\t(No symbol) [0x00007FF74636909A]\n\t(No symbol) [0x00007FF74633E13D]\n\t(No symbol) [0x00007FF7463867E8]\n\t(No symbol) [0x00007FF746368E03]\n\t(No symbol) [0x00007FF74633D686]\n\t(No symbol) [0x00007FF74633CB92]\n\t(No symbol) [0x00007FF74633D4B3]\n\t(No symbol) [0x00007FF74643900D]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF746446943+41539]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74643FC79+13689]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF7466BA04A+290794]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746537D31+497697]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530BB4+468644]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530D03+468979]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746522E16+411910]\n\tBaseThreadInitThunk [0x00007FFA2EDB53E0+16]\n\tRtlUserThreadStart [0x00007FFA3014485B+43]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.common.exceptions import NoSuchElementException # \n",
    "import pandas as pd\n",
    "import csv\n",
    "import os\n",
    "\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫\\\\文献爬虫数据') # 设置当前工作目录为你要保存文件的目录\n",
    "keyword='社会责任报告语调'\n",
    "# 初始化WebDriver\n",
    "driver = webdriver.Edge()\n",
    "url = 'https://www.cnki.net/'\n",
    "# 知网高级检索链接\n",
    "#url = 'https://kns.cnki.net/kns8s/AdvSearch?crossids=YSTTe4HG0%2CLSTPFY1C%2CJUP3MUPD%2CMPMFIG1A%2CWQ0UVIAA%2CBLZOG7CK%2CPWFIRAGL%2CEMRPGLPA%2CNLBO1Z6R%2CNN3FJMUV'\n",
    "driver.get(url)\n",
    "time.sleep(2)\n",
    "#普通检索\n",
    "driver.find_element(By.XPATH, '//*[@id=\"txt_SearchText\"]').click()\n",
    "driver.find_element(By.XPATH, '//*[@id=\"txt_SearchText\"]').send_keys(keyword) \n",
    "driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[1]/input[2]').click() #点击检索\n",
    "time.sleep(4)\n",
    "# 高级检索\n",
    "#driver.find_element(By.XPATH, '//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').click()\n",
    "\n",
    "#driver.find_element(By.XPATH,'//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').send_keys(keyword) #检索词\n",
    "#time.sleep(2)\n",
    "\n",
    "# 进行检索//*  \n",
    "#driver.find_element(By.XPATH,'//*[@id=\"gradetxt\"]/dd[2]/a').click()                   \n",
    "#driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[1]/div/div[2]/div/div[1]/div/div[1]/div[2]/div[3]/input').click()\n",
    "time.sleep(4)\n",
    "#cssci\n",
    "#driver.find_element(By.XPATH,'//*[@id=\"divGroup\"]/dl[2]/dd/div/ul/li[2]/input').click()\n",
    "#cscd\n",
    "#time.sleep(2)\n",
    "#driver.find_element(By.XPATH,'//*[@id=\"divGroup\"]/dl[2]/dd/div/ul/li[5]/input').click()\n",
    "\n",
    "\n",
    "\n",
    "# 每页显示条数改成50\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/div/i').click()\n",
    "time.sleep(1)\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/ul/li[3]').click()\n",
    "\n",
    "#只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click()\n",
    "\n",
    "#time.sleep(2)\n",
    "# 点击“学术论文”\n",
    "\n",
    "# 创建空列表以存储所有元素的文本内容\n",
    "column1_texts = []\n",
    "column2_texts = []\n",
    "column3_texts = []\n",
    "column4_texts = []\n",
    "column5_texts = []\n",
    "column6_texts = []\n",
    "column7_texts = []\n",
    "a=1\n",
    "# 循环遍历每一页\n",
    "while True:\n",
    "    # 获取当前页的元素数量  \n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr'))\n",
    "    if elements_count != 50:\n",
    "        print(f\"当前页{a}元素数量: {elements_count}，不是50，可能是最后一页\")\n",
    "    # 遍历当前页的元素，获取文本内容并存储到列表中\n",
    "    for i in range(1, elements_count + 1):\n",
    "        # 第一列 文章题目      //*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[1]/td[2]/a\n",
    "        try:\n",
    "            element = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[2]/a')\n",
    "            column1_texts.append(element.text)\n",
    "        except NoSuchElementException:\n",
    "            column1_texts.append(\"NA\")\n",
    "\n",
    "        try:\n",
    "            column7_texts.append(driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[2]/a').get_attribute('href'))\n",
    "        except NoSuchElementException:\n",
    "            column7_texts.append(\"NA\")\n",
    "        # 第二列 作者      \n",
    "        try:\n",
    "            #element_col2 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[3]')\n",
    "            column2_texts.append(driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[3]').text)\n",
    "        except NoSuchElementException:\n",
    "            column2_texts.append(\"NA\")\n",
    "\n",
    "        # 第三列 期刊        \n",
    "        try:\n",
    "            #element_col3 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[4]/span/a')\n",
    "            column3_texts.append(driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[4]/span/a').text)\n",
    "        except NoSuchElementException:\n",
    "            column3_texts.append(\"NA\")\n",
    "\n",
    "        # 第四列 时间        \n",
    "        #try:\n",
    "            #element_col4 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[5]')\n",
    "            #column4_texts.append(driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[5]').text)\n",
    "       # except NoSuchElementException:\n",
    "           # column4_texts.append(\"NA\")\n",
    "\n",
    "      \n",
    "\n",
    "\n",
    "    time.sleep(2)  # 等待页面加载\n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        print(f'正在爬一轮第{a}页')\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        print('爬取结束')\n",
    "        break  # 如果找不到下一页按钮，跳出循环 \n",
    "driver.quit()# 关闭浏览器 \n",
    "df1=pd.DataFrame({\n",
    "    '题名': column1_texts,\n",
    "    '链接': column7_texts,\n",
    "    '作者': column2_texts,\n",
    "    '期刊名称': column3_texts,\n",
    "    '时间': column4_texts,\n",
    "    })\n",
    "\n",
    "\n",
    "#读期刊目录\n",
    "excel_df = pd.read_excel(r'D:\\东财中文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df1, excel_df, on='期刊名称', how='left')\n",
    "from datetime import datetime\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_中文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e30bc17",
   "metadata": {},
   "source": [
    "# 爬外文文献（不包含摘要关键词）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b86bc043",
   "metadata": {},
   "outputs": [
    {
     "ename": "NoSuchWindowException",
     "evalue": "Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: MicrosoftEdge=135.0.3179.73)\nStacktrace:\n\tGetHandleVerifier [0x00007FF7465D7A55+24981]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74652AC60+444240]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF74685E0EA+2011274]\n\t(No symbol) [0x00007FF7462DC2B5]\n\t(No symbol) [0x00007FF74636EBF0]\n\t(No symbol) [0x00007FF7463861EA]\n\t(No symbol) [0x00007FF746368E03]\n\t(No symbol) [0x00007FF74633D686]\n\t(No symbol) [0x00007FF74633CB92]\n\t(No symbol) [0x00007FF74633D4B3]\n\t(No symbol) [0x00007FF74643900D]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF746446943+41539]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74643FC79+13689]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF7466BA04A+290794]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746537D31+497697]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530BB4+468644]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530D03+468979]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746522E16+411910]\n\tBaseThreadInitThunk [0x00007FFA2EDB53E0+16]\n\tRtlUserThreadStart [0x00007FFA3014485B+43]\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNoSuchWindowException\u001b[0m                     Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[74], line 20\u001b[0m\n\u001b[0;32m     18\u001b[0m keyword\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mesg\u001b[39m\u001b[38;5;124m'\u001b[39m \n\u001b[0;32m     19\u001b[0m \u001b[38;5;66;03m# 点击知网检索输入栏，输入你的检索词\u001b[39;00m\n\u001b[1;32m---> 20\u001b[0m \u001b[43mdriver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfind_element\u001b[49m\u001b[43m(\u001b[49m\u001b[43mBy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mXPATH\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m//*[@id=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgradetxt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m]/dd[1]/div[2]/input\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mclick()\n\u001b[0;32m     21\u001b[0m driver\u001b[38;5;241m.\u001b[39mfind_element(By\u001b[38;5;241m.\u001b[39mXPATH,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m//*[@id=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgradetxt\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m]/dd[1]/div[2]/input\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39msend_keys(keyword) \u001b[38;5;66;03m#检索词\u001b[39;00m\n\u001b[0;32m     22\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m3\u001b[39m)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:770\u001b[0m, in \u001b[0;36mWebDriver.find_element\u001b[1;34m(self, by, value)\u001b[0m\n\u001b[0;32m    767\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m NoSuchElementException(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot locate relative element with: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mby\u001b[38;5;241m.\u001b[39mroot\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    768\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m elements[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m--> 770\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mCommand\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mFIND_ELEMENT\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43musing\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mby\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mvalue\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:384\u001b[0m, in \u001b[0;36mWebDriver.execute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    382\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcommand_executor\u001b[38;5;241m.\u001b[39mexecute(driver_command, params)\n\u001b[0;32m    383\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m response:\n\u001b[1;32m--> 384\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43merror_handler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    385\u001b[0m     response[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_unwrap_value(response\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[0;32m    386\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py:232\u001b[0m, in \u001b[0;36mErrorHandler.check_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    230\u001b[0m         alert_text \u001b[38;5;241m=\u001b[39m value[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124malert\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    231\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace, alert_text)  \u001b[38;5;66;03m# type: ignore[call-arg]  # mypy is not smart enough here\u001b[39;00m\n\u001b[1;32m--> 232\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace)\n",
      "\u001b[1;31mNoSuchWindowException\u001b[0m: Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: MicrosoftEdge=135.0.3179.73)\nStacktrace:\n\tGetHandleVerifier [0x00007FF7465D7A55+24981]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74652AC60+444240]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF74685E0EA+2011274]\n\t(No symbol) [0x00007FF7462DC2B5]\n\t(No symbol) [0x00007FF74636EBF0]\n\t(No symbol) [0x00007FF7463861EA]\n\t(No symbol) [0x00007FF746368E03]\n\t(No symbol) [0x00007FF74633D686]\n\t(No symbol) [0x00007FF74633CB92]\n\t(No symbol) [0x00007FF74633D4B3]\n\t(No symbol) [0x00007FF74643900D]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF746446943+41539]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74643FC79+13689]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF7466BA04A+290794]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746537D31+497697]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530BB4+468644]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746530D03+468979]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF746522E16+411910]\n\tBaseThreadInitThunk [0x00007FFA2EDB53E0+16]\n\tRtlUserThreadStart [0x00007FFA3014485B+43]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.common.exceptions import NoSuchElementException\n",
    "import pandas as pd\n",
    "\n",
    "import os\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫\\\\文献爬虫数据') # 设置当前工作目录为你要保存文件的目录\n",
    "\n",
    "# 初始化WebDriver\n",
    "driver = webdriver.Edge()\n",
    "url = 'https://kns.cnki.net/kns8s/AdvSearch?crossids=YSTT4HG0%2CLSTPFY1C%2CJUP3MUPD%2CMPMFIG1A%2CWQ0UVIAA%2CBLZOG7CK%2CPWFIRAGL%2CEMRPGLPA%2CNLBO1Z6R%2CNN3FJMUV'\n",
    "driver.get(url)\n",
    "time.sleep(2)\n",
    "\n",
    "keyword='esg' \n",
    "# 点击知网检索输入栏，输入你的检索词\n",
    "driver.find_element(By.XPATH, '//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"gradetxt\"]/dd[1]/div[2]/input').send_keys(keyword) #检索词\n",
    "time.sleep(3)\n",
    "\n",
    "# 进行检索\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[1]/div/div[2]/div/div[1]/div/div[1]/div[2]/div[3]/input').click()\n",
    "time.sleep(4)\n",
    "# 每页显示条数改成50\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/div/i').click()\n",
    "driver.find_element(By.XPATH,'//*[@id=\"perPageDiv\"]/ul/li[3]/a').click()\n",
    "time.sleep(3)\n",
    "\n",
    "\n",
    "#只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 点外文\n",
    "driver.find_element(By.XPATH,'//*[@id=\"ModuleSearch\"]/div[2]/div/div/div/div/a[2]').click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 创建空列表以存储所有元素的文本内容\n",
    "column1_texts = []\n",
    "column2_texts = []\n",
    "column3_texts = []\n",
    "column4_texts = []\n",
    "column5_texts = []\n",
    "\n",
    "a=1\n",
    "# 循环遍历每一页\n",
    "while True:\n",
    "    # 获取当前页的元素数量  //*[@id=\"gridTable\"]/div/div/div/table/tbody\n",
    "    elements_count = len(driver.find_elements(By.XPATH, '//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr'))\n",
    "    if elements_count != 50:\n",
    "        print(f\"当前页{a}元素数量: {elements_count}，不是50，可能是最后一页\")\n",
    "    # 遍历当前页的元素，获取文本内容并存储到列表中\n",
    "    for i in range(1, elements_count + 1):\n",
    "        # 第一列 文章题目    \n",
    "        try:\n",
    "            element_col1 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[2]/a')\n",
    "            column1_texts.append(element_col1.text)\n",
    "            column5_texts.append(element_col1.get_attribute('href')) # 获取链接\n",
    "        except NoSuchElementException:\n",
    "            column1_texts.append(\"NA\")\n",
    "\n",
    "        # 第二列 作者      \n",
    "        try:\n",
    "            element_col2 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[3]')\n",
    "            column2_texts.append(element_col2.text)\n",
    "        except NoSuchElementException:\n",
    "            column2_texts.append(\"NA\")\n",
    "\n",
    "        # 第三列 期刊        \n",
    "        try:\n",
    "            element_col3 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[4]')\n",
    "            column3_texts.append(element_col3.text)\n",
    "        except NoSuchElementException:\n",
    "            column3_texts.append(\"NA\")\n",
    "\n",
    "        # 第四列 时间        \n",
    "        try:\n",
    "            element_col4 = driver.find_element(By.XPATH, f'//*[@id=\"gridTable\"]/div/div/div/table/tbody/tr[{i}]/td[5]')\n",
    "            column4_texts.append(element_col4.text)\n",
    "        except NoSuchElementException:\n",
    "            column4_texts.append(\"NA\")\n",
    "\n",
    "\n",
    "\n",
    "    # 尝试点击下一页按钮\n",
    "    try:\n",
    "        driver.find_element(By.ID, 'PageNext').click()\n",
    "        print(f'正在爬起第{a}页')\n",
    "        a += 1\n",
    "        time.sleep(3)  # 等待页面加载 \n",
    "    except NoSuchElementException:\n",
    "        break  # 如果找不到下一页按钮，跳出循环 \n",
    "    \n",
    "df1=pd.DataFrame({\n",
    "    '题名': column1_texts,\n",
    "    '链接': column5_texts,\n",
    "    '作者': column2_texts,\n",
    "    '期刊名称': column3_texts,\n",
    "    '时间': column4_texts,\n",
    "    })\n",
    "driver.quit()# 关闭浏览器\n",
    "excel_df = pd.read_excel(r'D:\\东财外文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df1, excel_df, on='期刊名称', how='left')\n",
    "\n",
    "\n",
    "\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "from datetime import datetime\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_外文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d3ef4dc",
   "metadata": {},
   "source": [
    "# Drissionpage 中文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e224dec6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "爬取第1页\n",
      "爬取第2页\n",
      "爬取第3页\n",
      "爬取第4页\n",
      "爬取第5页\n",
      "爬取第6页\n",
      "爬取第7页\n",
      "爬取第8页\n",
      "爬取第9页\n",
      "爬取第10页\n",
      "爬取第11页\n",
      "爬取第12页\n",
      "爬取第13页\n",
      "爬取第14页\n",
      "爬取第15页\n",
      "爬取第16页\n",
      "爬取第17页\n",
      "爬取第18页\n",
      "爬取第19页\n",
      "爬取第20页\n",
      "爬取第21页\n",
      "爬取第22页\n",
      "爬取第23页\n",
      "爬取第24页\n",
      "爬取第25页\n",
      "爬取第26页\n",
      "爬取第27页\n",
      "爬取第28页\n",
      "爬取第29页\n",
      "爬取第30页\n",
      "爬取第31页\n",
      "爬取第32页\n",
      "爬取第33页\n",
      "爬取第34页\n",
      "爬取第35页\n",
      "爬取第36页\n",
      "爬取第37页\n",
      "爬取第38页\n",
      "爬取第39页\n",
      "爬取第40页\n",
      "爬取第41页\n",
      "爬取第42页\n",
      "爬取第43页\n"
     ]
    }
   ],
   "source": [
    "from DrissionPage import ChromiumOptions, ChromiumPage\n",
    "import pandas as pd\n",
    "import time\n",
    "import os\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫') # 设置当前工作目录为你要保存文件的目录\n",
    "# 打开网页\n",
    "path=r'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'  # 请改为你电脑内Chrome可执行文件路径\n",
    "co = ChromiumOptions().set_browser_path(path)\n",
    "page = ChromiumPage(co)\n",
    "\n",
    "keyword='社会责任披露'\n",
    "\n",
    "page.get('https://www.cnki.net/')\n",
    "page.ele('x://*[@id=\"txt_SearchText\"]').input(keyword)\n",
    "page.ele('x://*[@class=\"search-btn\"]').click() #点击检索\n",
    "\n",
    "page.ele('x://*[@id=\"perPageDiv\"]/div/i').click() #点击每页显示条数\n",
    "page.ele('x://*[@id=\"perPageDiv\"]/ul/li[3]').click() #选择50条\n",
    "page.ele('x://*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click() #只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "\n",
    "\n",
    "# 遍历当前页的元素，获取文本内容并存储到列表中 \n",
    "titles, authors, journals, times, links = [], [], [], [], [] #创建空列表\n",
    "a=0\n",
    "while True:\n",
    "   title=page.eles('x://*[@class=\"fz14\"]') #获取题目\n",
    "   author=page.eles('x://*[@class=\"author\"]') #获取作者\n",
    "   journal=page.eles('x://*[@class=\"source\"]') #获取期刊\n",
    "   time=page.eles('x://*[@class=\"date\"]') #获取时间\n",
    "   for i in zip(title, author, journal, time):\n",
    "        titles.append(i[0].text) #获取题目文本\n",
    "        authors.append(i[1].text) #获取作者文本     \n",
    "        journals.append(i[2].text) #获取期刊文本\n",
    "        times.append(i[3].text) #获取时间文本           \n",
    "        links.append(i[0].attr('href')) #获取链接文本\n",
    "   try:\n",
    "        page.ele('x://*[@id=\"PageNext\"]').click() #点击下一页\n",
    "        a += 1 \n",
    "        print(f'爬取第{a}页')\n",
    "   except Exception as e:\n",
    "        break\n",
    "\n",
    "df1=pd.DataFrame({\n",
    "    '题名': titles,\n",
    "    '链接': links,\n",
    "    '作者': authors,\n",
    "    '期刊名称': journals,\n",
    "    '时间': times\n",
    "})\n",
    "\n",
    "\n",
    "#读期刊目录\n",
    "excel_df = pd.read_excel(r'D:\\东财中文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df1, excel_df, on='期刊名称', how='left')\n",
    "from datetime import datetime\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_中文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)\n",
    "page.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d83dc34d",
   "metadata": {},
   "source": [
    "# 外文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "64cbad77",
   "metadata": {},
   "outputs": [
    {
     "ename": "NoRectError",
     "evalue": "\n该元素没有位置及大小。\n版本: 4.1.0.18",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNoRectError\u001b[0m                               Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 23\u001b[0m\n\u001b[0;32m     21\u001b[0m page\u001b[38;5;241m.\u001b[39mele(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx://*[@id=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModuleSearch\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m]/div[2]/div/div/div/div/a[2]\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mclick() \u001b[38;5;66;03m#点击外文\u001b[39;00m\n\u001b[0;32m     22\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m---> 23\u001b[0m \u001b[43mpage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mele\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mx://*[@id=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mperPageDiv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m]/div\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclick\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m#点击每页显示条数\u001b[39;00m\n\u001b[0;32m     24\u001b[0m page\u001b[38;5;241m.\u001b[39mele(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx://*[@id=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mperPageDiv\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m]/ul/li[3]\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mclick() \u001b[38;5;66;03m#选择50条\u001b[39;00m\n\u001b[0;32m     25\u001b[0m page\u001b[38;5;241m.\u001b[39mele(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx://*[@id=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModuleSearch\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m]/div[2]/div/div/ul/li[1]\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mclick() \u001b[38;5;66;03m#只获取学术论文，排除学位论文、会议论文、报纸等\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\clicker.py:23\u001b[0m, in \u001b[0;36mClicker.__call__\u001b[1;34m(self, by_js, timeout, wait_stop)\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, by_js\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1.5\u001b[39m, wait_stop\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[1;32m---> 23\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mleft\u001b[49m\u001b[43m(\u001b[49m\u001b[43mby_js\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwait_stop\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\clicker.py:71\u001b[0m, in \u001b[0;36mClicker.left\u001b[1;34m(self, by_js, timeout, wait_stop)\u001b[0m\n\u001b[0;32m     68\u001b[0m     \u001b[38;5;28;01melif\u001b[39;00m by_js \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[0;32m     69\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m NoRectError\n\u001b[1;32m---> 71\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m can_click \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ele\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstates\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_in_viewport\u001b[49m:\n\u001b[0;32m     72\u001b[0m     by_js \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m     74\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m can_click \u001b[38;5;129;01mand\u001b[39;00m (by_js \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_ele\u001b[38;5;241m.\u001b[39mstates\u001b[38;5;241m.\u001b[39mis_covered):\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\states.py:44\u001b[0m, in \u001b[0;36mElementStates.is_in_viewport\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     42\u001b[0m \u001b[38;5;129m@property\u001b[39m\n\u001b[0;32m     43\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mis_in_viewport\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m---> 44\u001b[0m     x, y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ele\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclick_point\u001b[49m\n\u001b[0;32m     45\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m location_in_viewport(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_ele\u001b[38;5;241m.\u001b[39mowner, x, y) \u001b[38;5;28;01mif\u001b[39;00m x \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\rect.py:43\u001b[0m, in \u001b[0;36mElementRect.click_point\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     41\u001b[0m \u001b[38;5;129m@property\u001b[39m\n\u001b[0;32m     42\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mclick_point\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m---> 43\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_page_coord(\u001b[38;5;241m*\u001b[39m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mviewport_click_point\u001b[49m)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\rect.py:57\u001b[0m, in \u001b[0;36mElementRect.viewport_click_point\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     55\u001b[0m \u001b[38;5;129m@property\u001b[39m\n\u001b[0;32m     56\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mviewport_click_point\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m---> 57\u001b[0m     m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_viewport_rect\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mpadding\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     58\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mviewport_midpoint[\u001b[38;5;241m0\u001b[39m], m[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m3\u001b[39m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_units\\rect.py:100\u001b[0m, in \u001b[0;36mElementRect._get_viewport_rect\u001b[1;34m(self, quad)\u001b[0m\n\u001b[0;32m     99\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_viewport_rect\u001b[39m(\u001b[38;5;28mself\u001b[39m, quad):\n\u001b[1;32m--> 100\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ele\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mowner\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run_cdp\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mDOM.getBoxModel\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbackendNodeId\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ele\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_backend_id\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m'\u001b[39m][quad]\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_pages\\chromium_base.py:384\u001b[0m, in \u001b[0;36mChromiumBase._run_cdp\u001b[1;34m(self, cmd, **cmd_args)\u001b[0m\n\u001b[0;32m    382\u001b[0m ignore \u001b[38;5;241m=\u001b[39m cmd_args\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m_ignore\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m    383\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdriver\u001b[38;5;241m.\u001b[39mrun(cmd, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mcmd_args)\n\u001b[1;32m--> 384\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m r \u001b[38;5;28;01mif\u001b[39;00m __ERROR__ \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m r \u001b[38;5;28;01melse\u001b[39;00m \u001b[43mraise_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbrowser\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mignore\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\DrissionPage\\_functions\\tools.py:199\u001b[0m, in \u001b[0;36mraise_error\u001b[1;34m(result, browser, ignore, user)\u001b[0m\n\u001b[0;32m    196\u001b[0m     r \u001b[38;5;241m=\u001b[39m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(_S\u001b[38;5;241m.\u001b[39m_lang\u001b[38;5;241m.\u001b[39mjoin(_S\u001b[38;5;241m.\u001b[39m_lang\u001b[38;5;241m.\u001b[39mUNKNOWN_ERR, INFO\u001b[38;5;241m=\u001b[39mresult, TIP\u001b[38;5;241m=\u001b[39m_S\u001b[38;5;241m.\u001b[39m_lang\u001b[38;5;241m.\u001b[39mFEEDBACK))\n\u001b[0;32m    198\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m ignore \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(r, ignore):\n\u001b[1;32m--> 199\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m r\n",
      "\u001b[1;31mNoRectError\u001b[0m: \n该元素没有位置及大小。\n版本: 4.1.0.18"
     ]
    }
   ],
   "source": [
    "\n",
    "from DrissionPage import ChromiumOptions, ChromiumPage\n",
    "import pandas as pd\n",
    "import time\n",
    "import os\n",
    "\n",
    "# 设置当前工作目录\n",
    "os.chdir('D:\\\\漂绿文献\\\\爬虫\\\\文献爬虫数据') # 设置当前工作目录为你要保存文件的目录\n",
    "# 打开网页\n",
    "path=r'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'  # 请改为你电脑内Chrome可执行文件路径\n",
    "co = ChromiumOptions().set_browser_path(path)\n",
    "page = ChromiumPage(co)\n",
    "\n",
    "\n",
    "keyword='esg语调'\n",
    "\n",
    "page.get('https://www.cnki.net/')\n",
    "page.ele('x://*[@id=\"txt_SearchText\"]').input(keyword)\n",
    "page.ele('x://*[@class=\"search-btn\"]').click() #点击检索\n",
    "time.sleep(4)\n",
    "#外文\n",
    "page.ele('x://*[@id=\"ModuleSearch\"]/div[2]/div/div/div/div/a[2]').click() #点击外文\n",
    "time.sleep(1)\n",
    "page.ele('x://*[@id=\"perPageDiv\"]/div').click() #点击每页显示条数\n",
    "page.ele('x://*[@id=\"perPageDiv\"]/ul/li[3]').click() #选择50条\n",
    "page.ele('x://*[@id=\"ModuleSearch\"]/div[2]/div/div/ul/li[1]').click() #只获取学术论文，排除学位论文、会议论文、报纸等\n",
    "\n",
    "\n",
    "         \n",
    "time.sleep(2)\n",
    "\n",
    "# 遍历当前页的元素，获取文本内容并存储到列表中 \n",
    "titles, authors, journals, times, links = [], [], [], [], [] #创建空列表\n",
    "a=0\n",
    "while True:\n",
    "   title=page.eles('x://*[@class=\"fz14\"]') #获取题目\n",
    "   author=page.eles('x://*[@class=\"author\"]') #获取作者\n",
    "   journal=page.eles('x://*[@class=\"source\"]') #获取期刊\n",
    "   time=page.eles('x://*[@class=\"date\"]') #获取时间\n",
    "   for i in zip(title, author, journal, time):\n",
    "        titles.append(i[0].text) #获取题目文本\n",
    "        authors.append(i[1].text) #获取作者文本     \n",
    "        journals.append(i[2].text) #获取期刊文本\n",
    "        times.append(i[3].text) #获取时间文本           \n",
    "        links.append(i[0].attr('href')) #获取链接文本\n",
    "   try:\n",
    "        page.ele('x://*[@id=\"PageNext\"]').click() #点击下一页\n",
    "        a += 1 \n",
    "        print(f'爬取第{a}页')\n",
    "   except Exception as e:\n",
    "        break\n",
    "\n",
    "import pandas as pd\n",
    "df1=pd.DataFrame({\n",
    "    '题名': titles,\n",
    "    '链接': links,\n",
    "    '作者': authors,\n",
    "    '期刊名称': journals,\n",
    "    '时间': times\n",
    "})\n",
    "\n",
    "\n",
    "#读期刊目录\n",
    "excel_df = pd.read_excel(r'D:\\东财外文期刊分类.xlsx')\n",
    "excel_df = excel_df[['期刊名称', '级别']]\n",
    "\n",
    "#为期刊配级\n",
    "df4= pd.merge(df1, excel_df, on='期刊名称', how='left')\n",
    "from datetime import datetime\n",
    "df4 = df4[['级别', '题名','链接', '作者', '期刊名称', '时间']]\n",
    "df4['级别'] = df4['级别'].fillna('其他') # 将缺失值填充为'其他'\n",
    "#删除重复值\n",
    "df4 = df4.drop_duplicates(subset=['题名', '链接', '作者', '期刊名称', '时间'], keep='first').reset_index(drop=True)\n",
    "\n",
    "today_date = datetime.now().strftime('%Y-%m-%d')\n",
    "file_excel = f'{keyword}_英文文献标级_{today_date}.xlsx'\n",
    "df4.to_excel(file_excel, index=False)\n",
    "page.quit() # 关闭浏览器"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3469fb08",
   "metadata": {},
   "source": [
    "# 翻译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ee401644",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在翻译第1个标题，翻译为：MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN  06 HOURS 38 MINUTES 23 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE\n",
      "正在翻译第2个标题，翻译为：MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN  06 HOURS 38 MINUTES 22 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 10\u001b[0m\n\u001b[0;32m      8\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m df[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m题名\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[1;32m---> 10\u001b[0m     title_chinese \u001b[38;5;241m=\u001b[39m \u001b[43mtranslator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranslate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     11\u001b[0m     title_chineses\u001b[38;5;241m.\u001b[39mappend(title_chinese)\n\u001b[0;32m     12\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m正在翻译第\u001b[39m\u001b[38;5;132;01m{\u001b[39;00ma\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m个标题，翻译为：\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtitle_chinese\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\translate\\translate.py:45\u001b[0m, in \u001b[0;36mTranslator.translate\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m     42\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m text\n\u001b[0;32m     44\u001b[0m text_list \u001b[38;5;241m=\u001b[39m wrap(text, TRANSLATION_API_MAX_LENGHT, replace_whitespace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m \u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprovider\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_translation\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext_wraped\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mtext_wraped\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mtext_list\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\translate\\translate.py:45\u001b[0m, in \u001b[0;36m<genexpr>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m     42\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m text\n\u001b[0;32m     44\u001b[0m text_list \u001b[38;5;241m=\u001b[39m wrap(text, TRANSLATION_API_MAX_LENGHT, replace_whitespace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprovider\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_translation\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext_wraped\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m text_wraped \u001b[38;5;129;01min\u001b[39;00m text_list)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\translate\\providers\\mymemory_translated.py:42\u001b[0m, in \u001b[0;36mMyMemoryProvider.get_translation\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m     41\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_translation\u001b[39m(\u001b[38;5;28mself\u001b[39m, text):\n\u001b[1;32m---> 42\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     44\u001b[0m     translation \u001b[38;5;241m=\u001b[39m data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mresponseData\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtranslatedText\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m     45\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m translation:\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\translate\\providers\\mymemory_translated.py:38\u001b[0m, in \u001b[0;36mMyMemoryProvider._make_request\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m     35\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39memail:\n\u001b[0;32m     36\u001b[0m     params[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mde\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39memail\n\u001b[1;32m---> 38\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mrequests\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbase_url\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     39\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\u001b[38;5;241m.\u001b[39mjson()\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\requests\\api.py:73\u001b[0m, in \u001b[0;36mget\u001b[1;34m(url, params, **kwargs)\u001b[0m\n\u001b[0;32m     62\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget\u001b[39m(url, params\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m     63\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Sends a GET request.\u001b[39;00m\n\u001b[0;32m     64\u001b[0m \n\u001b[0;32m     65\u001b[0m \u001b[38;5;124;03m    :param url: URL for the new :class:`Request` object.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     70\u001b[0m \u001b[38;5;124;03m    :rtype: requests.Response\u001b[39;00m\n\u001b[0;32m     71\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m---> 73\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mget\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\requests\\api.py:59\u001b[0m, in \u001b[0;36mrequest\u001b[1;34m(method, url, **kwargs)\u001b[0m\n\u001b[0;32m     55\u001b[0m \u001b[38;5;66;03m# By using the 'with' statement we are sure the session is closed, thus we\u001b[39;00m\n\u001b[0;32m     56\u001b[0m \u001b[38;5;66;03m# avoid leaving sockets open which can trigger a ResourceWarning in some\u001b[39;00m\n\u001b[0;32m     57\u001b[0m \u001b[38;5;66;03m# cases, and look like a memory leak in others.\u001b[39;00m\n\u001b[0;32m     58\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m sessions\u001b[38;5;241m.\u001b[39mSession() \u001b[38;5;28;01mas\u001b[39;00m session:\n\u001b[1;32m---> 59\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msession\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\requests\\sessions.py:589\u001b[0m, in \u001b[0;36mSession.request\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m    584\u001b[0m send_kwargs \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m    585\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtimeout\u001b[39m\u001b[38;5;124m\"\u001b[39m: timeout,\n\u001b[0;32m    586\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mallow_redirects\u001b[39m\u001b[38;5;124m\"\u001b[39m: allow_redirects,\n\u001b[0;32m    587\u001b[0m }\n\u001b[0;32m    588\u001b[0m send_kwargs\u001b[38;5;241m.\u001b[39mupdate(settings)\n\u001b[1;32m--> 589\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprep\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43msend_kwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    591\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resp\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\requests\\sessions.py:703\u001b[0m, in \u001b[0;36mSession.send\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m    700\u001b[0m start \u001b[38;5;241m=\u001b[39m preferred_clock()\n\u001b[0;32m    702\u001b[0m \u001b[38;5;66;03m# Send the request\u001b[39;00m\n\u001b[1;32m--> 703\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43madapter\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    705\u001b[0m \u001b[38;5;66;03m# Total elapsed time of the request (approximately)\u001b[39;00m\n\u001b[0;32m    706\u001b[0m elapsed \u001b[38;5;241m=\u001b[39m preferred_clock() \u001b[38;5;241m-\u001b[39m start\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\requests\\adapters.py:667\u001b[0m, in \u001b[0;36mHTTPAdapter.send\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    664\u001b[0m     timeout \u001b[38;5;241m=\u001b[39m TimeoutSauce(connect\u001b[38;5;241m=\u001b[39mtimeout, read\u001b[38;5;241m=\u001b[39mtimeout)\n\u001b[0;32m    666\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 667\u001b[0m     resp \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    668\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    669\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    670\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    671\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    672\u001b[0m \u001b[43m        \u001b[49m\u001b[43mredirect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    673\u001b[0m \u001b[43m        \u001b[49m\u001b[43massert_same_host\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    674\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    675\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    676\u001b[0m \u001b[43m        \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax_retries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    677\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    678\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    679\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    681\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (ProtocolError, \u001b[38;5;167;01mOSError\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m    682\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(err, request\u001b[38;5;241m=\u001b[39mrequest)\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\connectionpool.py:789\u001b[0m, in \u001b[0;36mHTTPConnectionPool.urlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)\u001b[0m\n\u001b[0;32m    786\u001b[0m response_conn \u001b[38;5;241m=\u001b[39m conn \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m release_conn \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m    788\u001b[0m \u001b[38;5;66;03m# Make the request on the HTTPConnection object\u001b[39;00m\n\u001b[1;32m--> 789\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    790\u001b[0m \u001b[43m    \u001b[49m\u001b[43mconn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    791\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    792\u001b[0m \u001b[43m    \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    793\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout_obj\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    794\u001b[0m \u001b[43m    \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    795\u001b[0m \u001b[43m    \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    796\u001b[0m \u001b[43m    \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    797\u001b[0m \u001b[43m    \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    798\u001b[0m \u001b[43m    \u001b[49m\u001b[43mresponse_conn\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresponse_conn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    799\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreload_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    800\u001b[0m \u001b[43m    \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdecode_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    801\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mresponse_kw\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    802\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    804\u001b[0m \u001b[38;5;66;03m# Everything went great!\u001b[39;00m\n\u001b[0;32m    805\u001b[0m clean_exit \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\connectionpool.py:495\u001b[0m, in \u001b[0;36mHTTPConnectionPool._make_request\u001b[1;34m(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[0;32m    492\u001b[0m \u001b[38;5;66;03m# conn.request() calls http.client.*.request, not the method in\u001b[39;00m\n\u001b[0;32m    493\u001b[0m \u001b[38;5;66;03m# urllib3.request. It also calls makefile (recv) on the socket.\u001b[39;00m\n\u001b[0;32m    494\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 495\u001b[0m     \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    496\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    497\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    498\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    499\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    500\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    501\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreload_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    502\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdecode_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    503\u001b[0m \u001b[43m        \u001b[49m\u001b[43menforce_content_length\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43menforce_content_length\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    504\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    506\u001b[0m \u001b[38;5;66;03m# We are swallowing BrokenPipeError (errno.EPIPE) since the server is\u001b[39;00m\n\u001b[0;32m    507\u001b[0m \u001b[38;5;66;03m# legitimately able to close the connection after sending a valid response.\u001b[39;00m\n\u001b[0;32m    508\u001b[0m \u001b[38;5;66;03m# With this behaviour, the received response is still readable.\u001b[39;00m\n\u001b[0;32m    509\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBrokenPipeError\u001b[39;00m:\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\connection.py:441\u001b[0m, in \u001b[0;36mHTTPConnection.request\u001b[1;34m(self, method, url, body, headers, chunked, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[0;32m    439\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m header, value \u001b[38;5;129;01min\u001b[39;00m headers\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m    440\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mputheader(header, value)\n\u001b[1;32m--> 441\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mendheaders\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    443\u001b[0m \u001b[38;5;66;03m# If we're given a body we start sending that in chunks.\u001b[39;00m\n\u001b[0;32m    444\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m chunks \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\http\\client.py:1251\u001b[0m, in \u001b[0;36mHTTPConnection.endheaders\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1249\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1250\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m CannotSendHeader()\n\u001b[1;32m-> 1251\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_output\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmessage_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mencode_chunked\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\http\\client.py:1011\u001b[0m, in \u001b[0;36mHTTPConnection._send_output\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1009\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\r\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_buffer)\n\u001b[0;32m   1010\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_buffer[:]\n\u001b[1;32m-> 1011\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmsg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1013\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m message_body \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1014\u001b[0m \n\u001b[0;32m   1015\u001b[0m     \u001b[38;5;66;03m# create a consistent interface to message_body\u001b[39;00m\n\u001b[0;32m   1016\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(message_body, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mread\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[0;32m   1017\u001b[0m         \u001b[38;5;66;03m# Let file-like take precedence over byte-like.  This\u001b[39;00m\n\u001b[0;32m   1018\u001b[0m         \u001b[38;5;66;03m# is needed to allow the current position of mmap'ed\u001b[39;00m\n\u001b[0;32m   1019\u001b[0m         \u001b[38;5;66;03m# files to be taken into account.\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\http\\client.py:951\u001b[0m, in \u001b[0;36mHTTPConnection.send\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m    949\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msock \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    950\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mauto_open:\n\u001b[1;32m--> 951\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    952\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    953\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m NotConnected()\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\connection.py:279\u001b[0m, in \u001b[0;36mHTTPConnection.connect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    278\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconnect\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 279\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_new_conn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    280\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tunnel_host:\n\u001b[0;32m    281\u001b[0m         \u001b[38;5;66;03m# If we're tunneling it means we're connected to our proxy.\u001b[39;00m\n\u001b[0;32m    282\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_connected_to_proxy \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\connection.py:199\u001b[0m, in \u001b[0;36mHTTPConnection._new_conn\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    194\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Establish a socket connection and set nodelay settings on it.\u001b[39;00m\n\u001b[0;32m    195\u001b[0m \n\u001b[0;32m    196\u001b[0m \u001b[38;5;124;03m:return: New socket connection.\u001b[39;00m\n\u001b[0;32m    197\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    198\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 199\u001b[0m     sock \u001b[38;5;241m=\u001b[39m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate_connection\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    200\u001b[0m \u001b[43m        \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_dns_host\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    201\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    202\u001b[0m \u001b[43m        \u001b[49m\u001b[43msource_address\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msource_address\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    203\u001b[0m \u001b[43m        \u001b[49m\u001b[43msocket_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msocket_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    204\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    205\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mgaierror \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m    206\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m NameResolutionError(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhost, \u001b[38;5;28mself\u001b[39m, e) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
      "File \u001b[1;32me:\\anaconda3\\envs\\practice\\lib\\site-packages\\urllib3\\util\\connection.py:73\u001b[0m, in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     71\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m source_address:\n\u001b[0;32m     72\u001b[0m     sock\u001b[38;5;241m.\u001b[39mbind(source_address)\n\u001b[1;32m---> 73\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msa\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     74\u001b[0m \u001b[38;5;66;03m# Break explicitly a reference cycle\u001b[39;00m\n\u001b[0;32m     75\u001b[0m err \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from translate import Translator\n",
    "import pandas as pd\n",
    "file_excel= r'D:\\漂绿文献\\爬虫\\文献爬虫数据\\漂绿_英文文献标级_2025-07-14.xlsx'  # 请改为你要翻译的Excel文件路径\n",
    "df = pd.read_excel(file_excel)\n",
    "\n",
    "translator = Translator(to_lang=\"zh\")\n",
    "title_chineses = []\n",
    "a = 1\n",
    "for i in df['题名']:\n",
    "    title_chinese = translator.translate(i)\n",
    "    title_chineses.append(title_chinese)\n",
    "    print(f\"正在翻译第{a}个标题，翻译为：{title_chinese}\")\n",
    "    a += 1\n",
    "\n",
    "df['中文题名'] = title_chineses\n",
    "df.to_excel(f'{file_excel[:-5]}_翻译版.xlsx', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "practice",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
